/**
 * @author raelzhu
 * @version $Revision: 1.12 $
 */
// request class
function Request() {
	this.requestForm = document.getElementById('legacy.requestForm');
	this.responseFrame = window.frames['reloadHandler'];
	//this.responseFrame = document.getElementById('legacy.responseFrame');
	if (!this.requestForm) {
		var oDiv = document.createElement('div');
		oDiv.style.display = 'none';
		var content = [];
		content.push('<form id="legacy.requestForm" method="post" target="reloadHandler"></form>');
		//content.push('<iframe id="legacy.responseFrame" name="reloadHandler">');
		oDiv.innerHTML = content.join('\n');
		document.body.appendChild(oDiv);
		this.requestForm = document.getElementById('legacy.requestForm');
		//this.responseFrame = document.getElementById('legacy.responseFrame');
	} else {
		this.requestForm.innerHTML = '';
	}
}

Request.prototype = {
	setContextPath : function(path) {
		this.requestForm.action = path + '/common/ReloadHandler.jsp';
	},
	
	setSchema : function(schema) {
		this.setParameter('schema', schema);
	},
	
	setQLName : function(qlname) {
		this.setParameter('qlname', qlname);
	},
	
	setParameter : function(name, value) {
		var field = document.createElement('input');
		field.type = 'hidden';
		field.name = name;
		field.value = value;
		this.requestForm.appendChild(field);
	},
	
	send : function() {
		this.requestForm.submit();
	}
};

// retrive data from response frame
function getResponseData(key) {
	var iFrame = window.frames['reloadHandler'];
	var fields = iFrame.document.getElementsByName(key);
	if (fields.length > 0) {
		return fields[0].value;
	}
	return null;
}

// error message
function getErrorMessage(messageCode, labelValue) {
	var messageValue = message[messageCode];
	if (messageValue == '') {
		return [messageCode, labelValue].join(',');
	}
	return messageValue.replace('%s1', labelValue.replace(/\'/g, "'"));
}

// Summary Prepare
function getPreparedSummaryForm(oForm, actionValue, orderByName) {
	oForm = oForm || {name: ''};
	var form = new Opal.Form({
		name: oForm.name,
		target: '_self'
	});
	form.addSimple('searchname', context.searchname);
	if (context.searchvalue) {
		form.addSimple('searchvalue', context.searchvalue);
	}
	if (context.searchtype == 'date') {
		context.searchvalue1 = context.searchDateValue1;
		context.searchvalue2 = context.searchDateValue2;
	}
	if (context.searchtype == 'date' || context.searchtype == 'integer' || context.searchtype == 'double') {
		form.addSimple('searchvalue', (context.searchvalue1 || '') + '^' + (context.searchvalue2 || ''));
	}
	if (context.searchvalue1) {
		form.addSimple('searchvalue1', context.searchvalue1);
		if (context.searchtype == 'date') {
			form.addSimple('searchDateValue1', context.searchDateValue1);
		}
	}
	if (context.searchvalue2) {
		form.addSimple('searchvalue2', context.searchvalue2);
		if (context.searchtype == 'date') {
			form.addSimple('searchDateValue2', context.searchDateValue2);
		}
	}
	
	switch(actionValue) {
		case 'orderby':
			form.addSimple('pageaction', 'OrderBy');
			form.addSimple('orderbycolumn', orderByName);
			if ((context.orderbycolumn || '').toUpperCase() == orderByName.toUpperCase()) {
				var orderByType = context.orderbytype || 'ASC';
				form.addSimple('orderbytype', (orderByType.toUpperCase() == 'ASC') ? 'DESC' : 'ASC');
			}
			break;
		case 'jumppage':
			form.addSimple('pageaction', 'Jump');
			var no = context.currentpage || 1;
			if (no <= 0) {
				no = 1;
			}
			if (no - (context.totalpage || 1) > 0) {
				no = context.totalpage || 1;
			}
			form.addSimple('jumppage', no);
			break;
		case 'pre':
			form.addSimple('pageaction', 'Pre');
			break;
		case 'next':
			form.addSimple('pageaction', 'Next');
			break;
		case 'first':
			form.addSimple('pageaction', 'Jump');
			form.addSimple('jumppage', -2);
			break;
		case 'last':
			form.addSimple('pageaction', 'Jump');
			form.addSimple('jumppage', -1);
			break;
		case 'search':
			form.addSimple('pageaction', 'OrderBy');
			break;
	}
	
	return form;
}
